//
//  VideoCell.m
//  Tang
//
//  Created by lanou on 15/12/3.
//  Copyright © 2015年 352324497@qq.com. All rights reserved.
//

#import "VideoCell.h"
#import "VideoCollectionCell.h"

#define Kspce (5 * KWidth / 414)
static NSString *cellForSec = @"cell";
@implementation VideoCell
- (void)dealloc
{
    _delegate = nil;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        WaterFlowLayout * layout = [[WaterFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((KWidth - 5* Kspce)/2, 0); // item 宽和高
        
        layout.numberOfColumns = 2; // 列数
        layout.interitemSpacing = Kspce; // 列间距
        layout.sectionInsets = UIEdgeInsetsMake(0, Kspce*2 , 0, Kspce*2);
        layout.delegate = self;

        
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KWidth, 0) collectionViewLayout:layout];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.scrollEnabled = NO;
        self.collectionView.backgroundColor = [UIColor whiteColor];
        
        [self.collectionView registerClass:[VideoCollectionCell class] forCellWithReuseIdentifier:cellForSec];
        [self.contentView addSubview:_collectionView];
        
    }
    return self;
}

- (void)setModel:(VideoViewModel *)model
{
    _model = model;
    
    CGFloat height = (model.sub_catArray.count/2 +1) * KHeightForItem + model.sub_catArray.count/2 * Kspce;
    self.collectionView.height = height ;
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _model.sub_catArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    /**/
    VideoCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellForSec forIndexPath:indexPath];
    
    
    
    
    cell.model = _model.sub_catArray[indexPath.item];
    
    return cell;
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    VideoViewDetailModel *model = _model.sub_catArray[indexPath.row];
   
    if ([self.delegate respondsToSelector:@selector(jumpVcWithModel:)])
    {
        [self.delegate jumpVcWithModel:model];
    }
}
+ (CGFloat)heightForCellWithModel:(VideoViewModel *)model
{
   return  (model.sub_catArray.count/2 +1) * KHeightForItem + model.sub_catArray.count/2 * 10* KWidth / 414;
}



- (CGFloat)collectionView:(UICollectionView *)collectionView waterFlowLayout:(WaterFlowLayout *)layout heightForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
   
    if (indexPath.row == 0)
    {
        return  KHeightForItem * 2 + Kspce ;
    }
    else
    {
        return KHeightForItem ;
    }
    
    
}




@end
